package com.uniview.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * created by lux 06609
 * 字符串操作工具类
 */
@Slf4j
public class StringUtils {
    /**
     * 判断字符串是否为null或者空格
     *
     * @param str
     * @return false不为空 ture为空
     */
    public static boolean checkIsStrNull(String str) {
        if (null != str && str.trim().length() > 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 判断字符串是否为null或者空格
     *
     * @param str
     * @return true不为空 false为空
     */
    public static boolean checkIsStrNotNull(String str) {
        if (null != str && str.trim().length() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 逗号拼接的字符串转list
     *
     * @param str:逗号拼接的字符串
     * @return resultList 返回字符串list
     */
    public static List<String> strToList(String str) {
        String[] strArr = str.split("\\,");
        List<String> resultList = new ArrayList<String>(Arrays.asList(strArr));
        return resultList;
    }

    /**
     * list转换成逗号拼接的字符串
     */
    public static String listToStr(List<String> list, char separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(separator);
        }
        return list.isEmpty() ? "" : sb.toString().substring(0, sb.toString().length() - 1);
    }


    /**
     * 字符串中文改为unicode格式
     *
     * @param str
     * @return
     */
    public static String chineseToUnicode(String str) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            int chr1 = (char) str.charAt(i);
            if (chr1 >= 19968 && chr1 <= 171941) {
                result.append("\\u" + Integer.toHexString(chr1));
            } else {
                result.append(str.charAt(i));
            }
        }
        String resultString = result.toString();
        try {
            resultString = new String(resultString.getBytes(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            log.error("不支持的编码格式", e);
        }

        return resultString;

    }

    /**
     * object转String
     *
     * @param obj
     */
    public static String obj2str(Object obj) {
        return null == obj ? null : obj.toString();
    }

    /**
     * object 转long
     */
    public static Long obj2Long(Object obj) {
        return null == obj ? null : Long.valueOf(obj.toString());
    }

    public static void main(String[] args) {
        String a = "1,2,3";
        String[] b = a.split("\\,");
        List<String> resultList = strToList(a);
        System.out.println(resultList);
        String c = listToStr(resultList, ',');
        System.out.println(c);
    }

}
